//
//  ShopCarTableViewCell.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/8/2.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "ShopCarTableViewCell.h"
#import "LineLabel.h"
#define contentHeight self.Height-8

@interface ShopCarTableViewCell ()

@property (nonatomic,strong)UIImageView *leftImageView;

@property (nonatomic,strong)UILabel *titleLabel;

@property (nonatomic,strong)UILabel *standardLabel;

@property (nonatomic,strong)UILabel *priceLabel;

@property (nonatomic,strong)UILabel *oldPriceLabel;


@end

@implementation ShopCarTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self loadContentViewUI];
    }
    return self;
}

- (void)loadContentViewUI {
    /*
     *leftButton;
     *leftImageView
     *titleLabel;
     *deleteButton;
     *standardLabel;
     *priceLabel;
     *numberLabel;
     */
    self.backgroundColor = RGB(234, 234, 234);
    
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = RGB(246, 246, 246);
    [self addSubview:topView];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.top.equalTo(self.mas_top);
        make.bottom.equalTo(self.mas_bottom).with.offset(-8);
    }];
    
    
    self.leftButton = [[UIButton alloc] init];
    
    [self.leftButton setImage:[UIImage imageNamed:@"gouwuche_2"] forState:UIControlStateNormal];
    [self.leftButton setImage:[UIImage imageNamed:@"gouwuche_0"] forState:UIControlStateSelected];
    [self.leftButton addTarget:self action:@selector(didClickLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:self.leftButton];
    [self addSubview:self.leftButton];
    self.leftButton.selected = self.isSelected;
    
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).with.offset(8);
        make.top.equalTo(topView.mas_top);
        make.bottom.equalTo(topView.mas_bottom);
        make.width.mas_equalTo((self.Height-8)/2);
    }];
    
    UIImageView *leftImageView = [[UIImageView alloc] init];
    leftImageView.backgroundColor = [UIColor orangeColor];
    [self addSubview:leftImageView];
    self.leftImageView = leftImageView;
    
    [leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftButton.mas_right).with.offset(8);
        make.top.equalTo(self.mas_top).with.offset(4);
        make.bottom.equalTo(self.mas_bottom).with.offset(-12);
        make.width.mas_equalTo(80);
    }];
    
    UIButton *deleteButton = [[UIButton alloc] init];
    [deleteButton setImage:[UIImage imageNamed:@"gouwuche_1"] forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(didClickDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
//    [deleteButton setBackgroundColor:[UIColor orangeColor]];
    self.deleteButton = deleteButton;
    [self addSubview:deleteButton];
    
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(-8);
        make.top.equalTo(self.mas_top).with.offset(4);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
    }];
    
    //数量加按钮
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"StandarsAdd"] forState:UIControlStateNormal];
    [addBtn setImage:[UIImage imageNamed:@"StandarsAdd"] forState:UIControlStateHighlighted];
    [addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:addBtn];
    
    //数量减按钮
    UIButton *cutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cutBtn setImage:[UIImage imageNamed:@"StandarsDel"] forState:UIControlStateNormal];
    [cutBtn setImage:[UIImage imageNamed:@"StandarsDel"] forState:UIControlStateHighlighted];
    [cutBtn addTarget:self action:@selector(cutBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cutBtn];
    
    //数量显示
    self.numberLabel = [[UILabel alloc]init];
    self.numberLabel.textAlignment = NSTextAlignmentCenter;
    self.numberLabel.text = @"1";
    self.numberLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.numberLabel];

    //数量加按钮
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-8);
        make.bottom.equalTo(self).offset(-12);
        make.height.equalTo(@20);
        make.width.equalTo(@20);
    }];
    
    //数量显示
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(addBtn.mas_left);
        make.bottom.equalTo(addBtn);
        make.width.equalTo(addBtn);
        make.height.equalTo(addBtn);
    }];
    
    
    
    //数量减按钮
    [cutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.numberLabel.mas_left);
        make.height.equalTo(addBtn);
        make.width.equalTo(addBtn);
        make.bottom.equalTo(addBtn);
    }];

    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.textColor = DEFAULT_COLOR;
    self.priceLabel.text = @"¥123456.00";
    self.priceLabel.font = [UIFont boldSystemFontOfSize:15];
    self.priceLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.priceLabel];

    CGFloat priceLabelWidth = WIDTH - 190;
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftImageView.mas_right).with.offset(8);
        make.bottom.equalTo(addBtn);
        make.width.mas_equalTo(priceLabelWidth*0.6);
//        make.right.equalTo(cutBtn.mas_left);
        make.height.mas_equalTo(20);
    }];
//    NSLog(@"%f",WIDTH-185);
 
    self.oldPriceLabel = [[LineLabel alloc]initWithFrame:CGRectMake(122+priceLabelWidth * 0.6 + 8, 74, priceLabelWidth * 0.4-16, 20)];
    self.oldPriceLabel.textColor = [UIColor grayColor];
    self.oldPriceLabel.text = @"￥200.00";
    self.oldPriceLabel.backgroundColor = [UIColor clearColor];
    self.oldPriceLabel.textAlignment = NSTextAlignmentCenter;
    self.oldPriceLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:self.oldPriceLabel];

//    [self.oldPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(cutBtn.mas_left);
//        make.bottom.equalTo(addBtn);
//        make.height.mas_equalTo(20);
//        make.width.mas_equalTo(priceLabelWidth * 0.4);
//    }];

    self.standardLabel = [[UILabel alloc] init];
    self.standardLabel.text = @"颜色:天蓝色,尺寸:M";
    if (iPhone5) {
        self.standardLabel.font = [UIFont systemFontOfSize:12];
    } else {
        self.standardLabel.font = [UIFont systemFontOfSize:14];
    }
    [self addSubview:self.standardLabel];
    
    [self.standardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceLabel);
        make.bottom.equalTo(self.priceLabel.mas_top);
        make.right.equalTo(deleteButton.mas_left);
        make.height.equalTo(self.priceLabel);
    }];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @" 爱丽缇 新款女装夏 百搭波点印花半身裙子 显瘦A字裙 女J1218";
    if (iPhone5) {
        self.titleLabel.font = [UIFont systemFontOfSize:12];
    } else {
        self.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    self.titleLabel.numberOfLines = 0;
    [self addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(4);
        make.right.equalTo(deleteButton.mas_left);
        make.left.equalTo(leftImageView.mas_right).with.offset(4);
        make.bottom.equalTo(self.standardLabel.mas_top);
    }];
}


/**
 *  左边Button点击方法
 */
- (void)didClickLeftButton :(UIButton *)leftButton {
    leftButton.selected = !leftButton.selected;
    if (self.cartBlock) {
        self.cartBlock(leftButton.selected);
    }
}

- (void)didClickDeleteButton:(UIButton *)deleteButton {
    if ([self.delegate respondsToSelector:@selector(didClickDeleteButton:)]) {
        [self.delegate didClickDeleteButton:deleteButton];
    }
}

// 数量加按钮
-(void)addBtnClick
{
    if (self.numAddBlock) {
        self.numAddBlock();
    }
}

//数量减按钮
-(void)cutBtnClick
{
    if (self.numCutBlock) {
        self.numCutBlock();
    }
}

- (void)reloadDataWith:(ProductInfo *)model {
    /*
     *leftImageView;
     *titleLabel;
     *deleteButton;
     *standardLabel;
     *priceLabel;
     *oldPriceLabel;
     */
    
    NSString *string = [NSString stringWithFormat:@"%@%@",model.picprex[@"m"],model.picurl];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"businessNopict.jpg"]];
    
    self.titleLabel.text = model.title;
    
    NSString *standard;
    
    if (model.yanse.length == 0 && model.chima.length == 0) {
        standard = @"";
    } else {
        standard = [NSString stringWithFormat:@"颜色:%@ 尺码:%@",model.yanse,model.chima];
    }
  
    self.standardLabel.text = standard;
    
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@",model.jiage];
    
    self.numberLabel.text = [NSString stringWithFormat:@"%ld",model.nums];
    
    self.leftButton.selected = self.selected;
    
    /*
     
     [picurl]: 20140709042106.jpg
     [jzh]: 0
     [jiage]: 101
     [chima]: S
     [title]: 爱丽缇 新款女装夏 百搭波点印花半身裙子 显瘦A字裙 女J1218
     [yanse]: 天蓝色
     [nums]: 1
     [replaceId]: 11899
     [picprex]: <PictureModel>
     [m]: http://www.ylgw8.com/Uploads/sppic/
     [s]: http://www.ylgw8.com/Uploads/sppic/s_
     </PictureModel>
     [feilv]:
     [pid]: 16257

     
     */
    
//    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"businessNopict.jpg"]];
    
}

@end
